<?php

namespace app\erp\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Cartdb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';
    protected $pk = 'cart_id';
    protected $name = 'cart';
    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i',
        'delete_time' => 'timestamp:Y-m-d H:i',
        'pay_time' => 'timestamp:Y-m-d H:i'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;
    public $param = array();
    public $inset_ids = array();
    public $search_count = 0;

    public function goods()
    {
        return $this->hasOne('Goodsdb', 'goods_sku', 'goods_sku');
    }

    // 自动完成
    protected $insert = [
        'partner_id' => PARTNER_ID,
        'site_id',
        'is_checked' => 1,
        'purchase_state' => 2,
        'entry_state' => 2,
        'out_state' => 2,
        'pack_state' => 2,
        'receipt_state' => 2,
        'invoice' => 2,
        'judge_state' => 2,
        'pay_state' => 2,
        'invoice_state' => 2
    ];

    protected function setSiteIdAttr($value)
    {
        if (!$value) {
            return SITE_ID;
        }
        if ($value == 0) {
            return null;
        }
        return $value;
    }

    public function _emptyParam()
    {
        $this->param = array();
    }

    // 订单查询
    public function scopeOrderNo($query, $order_no)
    {
        if (is_array($order_no)) {
            $query->where('order_no', 'IN', $order_no);
        } else {
            if ($order_no) {
                $query->where('order_no', $order_no);
            } else {
                $query->where('order_no', '');
            }
        }
    }

    // 订单下的产品查询
    public function scopeGoodsSku($query, $goods_sku)
    {
        if (is_array($goods_sku)) {
            $query->where('goods_sku', 'IN', $goods_sku);
        } else {
            $query->where('goods_sku', $goods_sku);
        }
    }

    // 查询大包装台
    public function scopePackState($query, $pack_state)
    {
        $query->where('pack_state', $pack_state);
    }

    public function scopeReceiptState($query, $receipt_state)
    {
        $query->where('receipt_state', $receipt_state);
    }

    public function scopeCartId($query, $cart_id)
    {
        if (is_array($cart_id)) {
            $query->where('cart_id', 'IN', $cart_id);
        } else {
            $query->where('cart_id', $cart_id);
        }
    }

    public function scopeNoCartId($query, $no_cart_id)
    {
        $query->where('cart_id', 'not in', $no_cart_id);
    }

    public function scopeIsInstall($query, $is_install)
    {
        $query->where('is_install', $is_install);
    }

    public function scopeMemberId($query, $member_id)
    {
        $query->where('member_id', $member_id);
    }

    public function scopeJudgeState($query, $judge_state)
    {
        $query->where('judge_state', $judge_state);
    }

    public function scopeInvoiceState($query, $invoice_state)
    {
        $query->where('invoice_state', $invoice_state);
    }

    public function scopePurchaseState($query, $purchase_state)
    {
        $query->where('purchase_state', $purchase_state);
    }

    public function scopeEntryState($query, $entry_state)
    {
        $query->where('entry_state', $entry_state);
    }

    public function scopeOutState($query, $out_state)
    {
        $query->where('out_state', $out_state);
    }

    public function scopePayState($query, $pay_state)
    {
        $query->where('pay_state', $pay_state);
    }

    // 基础查询
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            if (isset ($this->param ['delete']) && $this->param ['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        /*$query->where ( 'partner_id', PARTNER_ID );
        if (isset ( $this->param ['site_id'] )) {
            if ($this->param ['site_id'] == 0 || $this->param ['site_id'] == '') {
                $query->where ( 'site_id', null );
            } elseif ($this->param ['site_id'] != - 1) {
                $query->where ( 'site_id', $this->param ['site_id'] );
            }
        } else {
            if (! empty ( SITE_ID )) {
                $query->where ( 'site_id', SITE_ID );
            } else {
                $query->where ( 'site_id', 'null' );
            }
        }*/
        if (isset ($this->param ['order_no'])) {
            $query->orderNo($this->param ['order_no']);
        }
        if (isset ($this->param ['goods_sku']) && $this->param ['goods_sku']) {
            $query->goodsSku($this->param ['goods_sku']);
        }
        if (isset ($this->param ['pack_state']) && $this->param ['pack_state']) {
            $query->packState($this->param ['pack_state']);
        }
        if (isset ($this->param ['receipt_state']) && $this->param ['receipt_state']) {
            $query->receiptState($this->param ['receipt_state']);
        }
        if (isset ($this->param ['cart_id']) && $this->param ['cart_id']) {
            $query->cartId($this->param ['cart_id']);
        }
        if (isset ($this->param ['is_install']) && $this->param ['is_install']) {
            $query->isInstall($this->param ['is_install']);
        }
        if (isset ($this->param ['member_id']) && $this->param ['member_id']) {
            $query->memberId($this->param ['member_id']);
        }
        if (isset ($this->param ['judge_state']) && $this->param ['judge_state']) {
            $query->judgeState($this->param ['judge_state']);
        }
        if (isset ($this->param ['invoice_state']) && $this->param ['invoice_state']) {
            $query->invoiceState($this->param ['invoice_state']);
        }
        if (isset ($this->param ['purchase_state']) && $this->param ['purchase_state']) {
            $query->purchaseState($this->param ['purchase_state']);
        }
        if (isset ($this->param ['entry_state']) && $this->param ['entry_state']) {
            $query->entryState($this->param ['entry_state']);
        }
        if (isset ($this->param ['out_state']) && $this->param ['out_state']) {
            $query->outState($this->param ['out_state']);
        }
        if (isset ($this->param ['no_cart_id']) && $this->param ['no_cart_id']) {
            $query->noCartId($this->param ['no_cart_id']);
        }
        if (isset ($this->param ['pay_state']) && $this->param ['pay_state']) {
            $query->payState($this->param ['pay_state']);
        }
    }

    public function aloneSave($data, $cart_ids)
    {
        $this->where('cart_id', 'IN', $cart_ids)->update($data);
        $this->inset_ids = $cart_ids;
        return 'success';
    }

    public function saveInvoiceState()
    {
        $this->where('order_no', 'IN', $this->param ['order_no'])->update(array(
            'invoice_state' => 1
        ), array(
            'order_no'
        ));
        return 'success';
    }

    public function ajaxCartIds()
    {
        $data = $this->master()->order('cart_id asc')->select()->toArray();
        $cart_ids = array();
        foreach ($data as $key => $val) {
            array_push($cart_ids, $val ['cart_id']);
        }
        return $cart_ids;
    }

    public function ajaxList()
    {
        $data = $this->master()->order('cart_id asc')->select()->toArray();
        if (isset ($this->param ['need_goods_info']) && $this->param ['need_goods_info'] == 'yes') {
            foreach ($data as $key => $val) {
                $get_temp = $this->handleData($val);
                $data [$key] = $get_temp;
            }
            return $data;
        } else {
            $cart_ids = array();
            $json_datas = array();
            $cart_id_arr = array();
            $sum_money = 0;
            foreach ($data as $key => $val) {
                array_push($cart_ids, $val ['goods_sku']);
                array_push($cart_id_arr, $val ['cart_id']);
                $temp['cart_id'] = $val['cart_id'];
                $temp['product_area'] = $val['product_area'];
                $temp['shuiwu_id'] = $val['shuiwu_id'];
                $temp['product_firm'] = $val['product_firm'];
                $temp['goods_unit'] = $val['goods_unit'];
                $temp ['goods_sku'] = $val ['goods_sku'];
                $temp ['sale_price'] = $val ['goods_price'];
                $temp['goods_keywords'] = $val['goods_keywords'];
                $temp['apply_state'] = $val['apply_state'];
                $temp['nopass_info'] = $val['nopass_info'];
                $temp ['num'] = $val ['goods_num'];
                $temp ['sale_attr_html'] = $val ['goods_attr_view'];
                $temp ['sale_attr'] = $val ['goods_attr'];
                $temp ['pack_state'] = $val ['pack_state'];
                $temp ['returned_state'] = $val ['returned_state'];
                $temp ['diy_beizhu'] = $val['diy_beizhu'];
                $temp['invoice_model'] = $val['invoice_model'] ?? '';
                if (isset ($this->param ['need_goods_title']) && $this->param ['need_goods_title'] == 'yes') {
                    $goodsdb = new Goodsdb ();
                    $goodsdb->param ['goods_sku'] = $val ['goods_sku'];
                    $goodsdb->param ['no_need_site'] = 'yes';
                    $goodsdb->param ['no_need_goods_handle'] = 'yes';
                    $goodsdb->param ['no_need_attr'] = 'yes';
                    $goodsdb->param ['need_cover'] = 'yes';
                    $get_goods = $goodsdb->ajaxFind();
                    if (isset ($get_goods ['goods_name'])) {
                        $temp ['goods_name'] = $get_goods ['goods_name'];
                        $temp ['goods_unit'] = $get_goods ['goods_unit'];
                        if ($temp['invoice_model'] == '') {
                            $temp ['invoice_model'] = $get_goods['invoice_model'];
                        }
                        $data [$key] ['goods_name'] = $get_goods ['goods_name'];
                        $data [$key] ['goods_unit'] = $get_goods ['goods_unit'];
                        $data [$key] ['sum_sale_price'] = number_format($val ['goods_price'] * $val ['goods_num'], 2, '.', '');
                    }
                } else {
                    if ($temp['invoice_model'] == '') {
                        $goodsdb = new Goodsdb ();
                        $temp ['invoice_model'] = $goodsdb->where('goods_sku', $val['goods_sku'])->value('invoice_model');
                    }
                }
                array_push($json_datas, $temp);
                $sum_money += $val ['goods_price'] * $val ['goods_num'];
            }
            $return ['list'] = $data;
            $return ['cart_ids'] = implode(',', $cart_ids);
            $return ['cart_id_arr'] = implode(',', $cart_id_arr);
            $return ['sum_price'] = $sum_money;
            $return ['json_data'] = json_encode($json_datas);
            return $return;
        }
    }

    public function ajaxSelect()
    {
        $data ['data'] = $this->ajaxList();
        $data ['count'] = $this->ajaxCount();
        return $data;
    }

    public function ajaxFind()
    {
        $data = $this->master()->find();
        if ($data) {
            $data = $data->toArray();
        } else {
            $data = array();
        }
        return $data;
    }

    public function ajaxCount()
    {
        $data = $this->master()->count();
        $this->search_count = $data;
        return $data;
    }

    // 批量修改，打包，出库等状态
    public function editCartState()
    {
        switch ($this->param ['field']) {
            case 'out_state' :
                // 出库
                $data ['out_state'] = 1;
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                $this->param ['out_state'] = 2;
                $this->ajaxCount();
                break;
            case 'entry_state' :
                // 入库
                $data ['entry_state'] = 1;
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                $this->param ['entry_state'] = 2;
                $this->ajaxCount();
                break;
            case 'purchase_state' :
                // 采购
                $data ['purchase_state'] = 1;
                $data ['depot_id'] = $this->param ['depot_id'];
                $data ['cost_price'] = $this->param ['cost_price'];
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                $this->param ['purchase_state'] = 2;
                $this->ajaxCount();
                break;
            case 'pack_id' :
                // $data['depot_id'] = $this->param['depot_id'];
                $data ['pack_id'] = $this->param ['value'];
                $data ['pack_state'] = 1;
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                // 查询是否还有未打包状态
                $this->param ['pack_state'] = 2;
                $this->ajaxCount();
                break;
            case 'receipt_state' :
                // 收货
                $data ['receipt_state'] = 1;
                $data ['receipt_time'] = time();
                if ($this->param ['is_exchange'] == 2) {
                    $data ['returned_state'] = 5;
                }
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                $this->param ['receipt_state'] = 2;
                $this->ajaxCount();
                break;
            case 'invoice_state' :
                // 开票
                $data ['invoice_state'] = 1;
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                $this->param ['invoice_state'] = 2;
                $this->ajaxCount();
                break;
            case 'pay_state' :
                // 支付状态
                $data ['pay_state'] = 1;
                $data ['pay_time'] = time();
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                $this->param ['pay_state'] = 2;
                $this->ajaxCount();
                break;
            case 'judge_state' :
                $data ['judge_state'] = 1;
                $this->where('cart_id', 'IN', $this->param ['cart_ids'])->update($data);
                $this->param ['judge_state'] = 2;
                $this->ajaxCount();
                break;
        }
    }

    // 设置退换货状态
    public function setReturned()
    {
        $info = $this->find();
        $this->save([
            'returned_state' => $this->param ['returned_state']
        ], [
            'cart_id' => $info ['cart_id']
        ]);
        // 日志
        switch ($this->param ['returned_state']) {
            case '2' :
                $text = '申请退货';
                break;
            case '3' :
                $text = '退货完成';
                break;
            case '4' :
                $text = '申请换货';
                break;
            case '5' :
                $text = '换货完成';
                break;
            case '6' :
                $text = '换货已发货';
                $this->save([
                    'pack_id' => $this->param ['express_id']
                ], [
                    'cart_id' => $info ['cart_id']
                ]);
                break;
            case '-1' :
                $text = '取消退换货';
                break;
        }
        $order_logdb = new OrderLogdb ();
        $order_logdb->param = array(
            'order_no' => $this->param ['order_no'],
            'member_id' => MEMBER_ID,
            'view_text' => '订单产品（' . $this->param ['goods_sku'] . '）' . $text,
            'post_json' => $this->param
        );
        return 'success';
    }

    // 单个修改 数量 价格等
    public function editCart()
    {
        if (isset ($this->param ['goods_sku']) && $this->param ['goods_sku'] != '') {
            $this->goodsSku($this->param ['goods_sku']);
        }
        $chk = $this->find();
        $order_logdb = new OrderLogdb ();
        $goodsdb = new Goodsdb ();
        $goodsdb->param ['goods_sku'] = $this->param ['goods_sku'];
        $goods_info = $goodsdb->ajaxFind();
        if (empty ($chk)) {
            // 没数据 新增
            foreach ($goods_info ['sale_price_info'] ['data'] as $k => $v) {
                if ($v ['sale_attr_price_name'] == '商品价格') {
                    $v ['sale_attr_price_name'] = '';
                }
                if ($v ['sale_attr_price_name'] == $this->param ['post_data'] ['sale_attr']) {
                    $data ['goods_sale_price'] = $v ['sale_price'];
                    $data ['goods_market_price'] = $v ['market_price'];
                    break;
                }
            }
            $data ['goods_sku'] = $this->param ['goods_sku'];
            $data ['goods_attr_view'] = $this->param ['post_data'] ['sale_attr_html'];
            $data ['goods_attr'] = $this->param ['post_data'] ['sale_attr'];
            $data ['goods_price'] = $this->param ['post_data'] ['sale_price'];
            $data ['goods_num'] = $this->param ['post_data'] ['num'];
            $data ['category_sign'] = $goods_info ['category_sign3'];
            $data ['is_install'] = $goods_info ['is_install'];
            $data ['order_no'] = $this->param ['order_no'];
            $this->allowField(true)->save($data);
            // 日志
            $order_logdb->param = array(
                'order_no' => $this->param ['order_no'],
                'member_id' => MEMBER_ID,
                'view_text' => '添加订单产品（' . $this->param ['goods_sku'] . '）',
                'post_json' => $this->param ['post_data']
            );
            $order_logdb->saveData();
        } else {
            $cart_info = $chk->toArray();
            if (!isset ($this->param ['post_data'] ['field'])) {
                // 删除
                $this->param ['cart_id'] = $cart_info ['cart_id'];
                $this->ajaxDelete();
                // 日志
                $order_logdb->param = array(
                    'order_no' => $this->param ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '删除订单产品（' . $cart_info ['goods_sku'] . '）',
                    'post_json' => $this->param ['post_data']
                );
                $order_logdb->saveData();
            } else {
                // 修改数据
                switch ($this->param ['post_data'] ['field']) {
                    case 'num' :
                        $this->save([
                            'goods_num' => $this->param ['post_data'] ['value']
                        ], [
                            'cart_id' => $cart_info ['cart_id']
                        ]);
                        // 日志
                        $order_logdb->param = array(
                            'order_no' => $this->param ['order_no'],
                            'member_id' => MEMBER_ID,
                            'view_text' => '修改订单产品（' . $cart_info ['goods_sku'] . '）数量为（' . $this->param ['post_data'] ['value'] . '）',
                            'post_json' => $this->param ['post_data']
                        );
                        $order_logdb->saveData();
                        break;
                    case 'sale_price' :
                        $this->save([
                            'goods_price' => $this->param ['post_data'] ['value']
                        ], [
                            'cart_id' => $cart_info ['cart_id']
                        ]);
                        // 日志
                        $order_logdb->param = array(
                            'order_no' => $this->param ['order_no'],
                            'member_id' => MEMBER_ID,
                            'view_text' => '修改订单产品（' . $cart_info ['goods_sku'] . '）售价为（' . $this->param ['post_data'] ['value'] . '）',
                            'post_json' => $this->param ['post_data']
                        );
                        $order_logdb->saveData();
                        break;
                    case 'sale_attr_html' :
                        $this->save([
                            'goods_attr_view' => $this->param ['post_data'] ['value']
                        ], [
                            'cart_id' => $cart_info ['cart_id']
                        ]);
                        // 日志
                        $order_logdb->param = array(
                            'order_no' => $this->param ['order_no'],
                            'member_id' => MEMBER_ID,
                            'view_text' => '设置订单产品（' . $cart_info ['goods_sku'] . '）属性为（' . $this->param ['post_data'] ['value'] . '）',
                            'post_json' => $this->param ['post_data']
                        );
                        $order_logdb->saveData();
                        break;
                    case 'sale_attr' :
                        $data ['goods_attr'] = $this->param ['post_data'] ['value'];
                        // 重置产品金额
                        foreach ($goods_info ['sale_price_info'] ['data'] as $k => $v) {
                            if ($v ['sale_attr_price_name'] == '商品价格') {
                                $v ['sale_attr_price_name'] = '';
                            }
                            if ($v ['sale_attr_price_name'] == $this->param ['post_data'] ['value']) {
                                $data ['goods_sale_price'] = $v ['sale_price'];
                                $data ['goods_market_price'] = $v ['market_price'];
                                break;
                            }
                        }
                        $this->save($data, [
                            'cart_id' => $cart_info ['cart_id']
                        ]);
                        break;
                    default :

                        break;
                }
            }
        }
        // 重新计算订单金额
        unset ($this->param ['goods_sku']);
        $order_money = $this->sumOrderMoney();
        return $order_money * 100;
    }

    public function sumOrderMoney()
    {
        $data = $this->order('cart_id asc')->master(true)->sum('goods_price*goods_num');
        return $data;
    }

    public function addCartHn()
    {
        $i = 0;
        $data = array();
        foreach ($this->param ['cart_info'] as $key => $val) {
            $sale_price = $val ['sale_price'];
            $data [$i] ['goods_sku'] = $val ['goods_sku'];
            $data [$i] ['goods_attr_view'] = $val ['sale_attr_html'];
            $data [$i] ['goods_attr'] = $val ['sale_attr'];
            $data [$i] ['goods_price'] = $sale_price;
            $data [$i] ['goods_num'] = $val ['num'];
            $data [$i] ['category_sign'] = $val ['category_sign3'];
            // $data[$i]['is_install'] = $goods_info['is_install'];
            $data [$i] ['order_no'] = $this->param ['order_no'];
            $data [$i] ['api_option'] = $val ['api_option'];
            if (isset ($val ['member_id'])) {
                $data [$i] ['member_id'] = $val ['member_id'];
            }

            $i++;
        }
        $insert_ids = $this->saveAll($data);
        $this->inset_ids = array();
        foreach ($insert_ids as $key => $val) {
            array_push($this->inset_ids, $val ['cart_id']);
        }
        return 'success';
    }

    // 处理添加
    public function addCart()
    {
        $i = 0;
        $data = array();
        $goodsdb = new Goodsdb ();
        foreach ($this->param ['cart_info'] as $key => $val) {
            $goodsdb->param ['goods_sku'] = $val ['goods_sku'];
            if (isset ($this->param ['no_need_goods_handle'])) {
                $goodsdb->param ['no_need_goods_handle'] = 'yes';
            }
            if (isset ($this->param ['no_need_attr'])) {
                $goodsdb->param ['no_need_attr'] = 'yes';
            }

            $goods_info = $goodsdb->ajaxFind();
            if (!isset ($goods_info ['goods_id'])) {
                return '没有找到商品';
            }
            $sale_price = 0;
            $data [$i] ['goods_price'] = $goods_info ['api_price'];
            $data [$i] ['goods_sku'] = $val ['goods_sku'];

            $data [$i] ['goods_attr_view'] = $val ['sale_attr_html'];
            $data [$i] ['goods_attr'] = $val ['sale_attr'];
            $data [$i] ['goods_price'] = $goods_info ['api_price'];
            $data [$i] ['goods_sale_price'] = $goods_info ['sale_price'];
            $data [$i] ['goods_num'] = $val ['num'];
            $data [$i] ['category_sign'] = $goods_info ['category_sign3'];
            $data [$i] ['is_install'] = $goods_info ['is_install'];
            $data [$i] ['order_no'] = $this->param ['order_no'];
            if (isset ($val ['member_id'])) {
                $data [$i] ['member_id'] = $val ['member_id'];
            }

            // 验证是否有相同的数据 如果有 则删除之前的数据
            $chk = $this->where('goods_sku', $val ['goods_sku'])->where('order_no', '')->where('goods_attr', $val ['sale_attr'])->find();
            if ($chk) {
                $this->param ['cart_id'] = $chk ['cart_id'];
                $this->ajaxDelete();
            }
            $i++;
        }
        $insert_ids = $this->saveAll($data);
        $this->inset_ids = array();
        foreach ($insert_ids as $key => $val) {
            array_push($this->inset_ids, $val ['cart_id']);
        }
        return 'success';
    }

    public function addCartNoChkgoods()
    {
        $i = 0;
        $data = array();
        $goodsdb = new Goodsdb ();
        foreach ($this->param ['cart_info'] as $key => $val) {
            $get_goods = $goodsdb->useGlobalScope(false)->where('goods_sku', $val ['goods_sku'])->find()->toArray();
            $data [$i] ['goods_sale_price'] = $val ['sale_price'];
            $data [$i] ['goods_market_price'] = $val ['sale_price'];
            $data [$i] ['goods_sku'] = $val ['goods_sku'];
            $data [$i] ['goods_attr_view'] = $val ['sale_attr_html'];
            $data [$i] ['goods_attr'] = $val ['sale_attr'];
            $data [$i] ['goods_price'] = $val ['sale_price'];
            $data [$i] ['goods_num'] = $val ['num'];
            $data [$i] ['category_sign'] = $get_goods ['category_sign3'];
            $data [$i] ['is_install'] = $get_goods ['is_install'];
            $data [$i] ['order_no'] = $this->param ['order_no'];
            if (isset ($val ['member_id'])) {
                $data [$i] ['member_id'] = $val ['member_id'];
            }

            // 验证是否有相同的数据 如果有 则删除之前的数据
            $chk = $this->where('goods_sku', $val ['goods_sku'])->where('order_no', '')->where('goods_attr', $val ['sale_attr'])->find();
            if ($chk) {
                $this->param ['cart_id'] = $chk ['cart_id'];
                $this->ajaxDelete();
            }
            $i++;
        }
        $insert_ids = $this->saveAll($data);
        $this->inset_ids = array();
        foreach ($insert_ids as $key => $val) {
            array_push($this->inset_ids, $val ['cart_id']);
        }
        return 'success';
    }

    // 软删除
    public function ajaxDelete()
    {
        if (isset ($this->param ['cart_id'])) {
            if (isset ($this->param ['delete']) && $this->param ['delete'] == 'yes') {
                $this->destroy(function ($query) {
                    $query->where('cart_id', 'IN', $this->param ['cart_id']);
                }, true);
            } else {
                $this->destroy(function ($query) {
                    $query->where('cart_id', 'IN', $this->param ['cart_id']);
                });
            }
            return 'success';
        }
    }

    // 购物车数量
    public function getCartNum()
    {
        $data = $this->sum('goods_num');
        return $data;
    }

    // 购物车销售金额
    public function setGoodsSaleNum()
    {
        $data = $this->select();
        $goodsdb = new Goodsdb ();
        foreach ($data as $key => $val) {
            $goodsdb->param ['site_id'] = $val ['site_id'];
            $goodsdb->param ['goods_sku'] = $val ['goods_sku'];
            $goodsdb->param ['add_sale_num'] = $val ['goods_num'];
            $goodsdb->ajaxSaveSaleNum();
        }
        return 'success';
    }

    protected function handleData($val)
    {
        foreach ($val as $k => $v) {
            $val [$k] = $v;
            switch ($k) {
                case 'goods_sku' :
                    $goodsdb = new Goodsdb ();
                    $goodsdb->param ['goods_sku'] = $val ['goods_sku'];
                    // $goodsdb->param['sale_state'] = 1;
                    if (isset ($this->param ['no_need_attr']) && $this->param ['no_need_attr'] == 'yes') {
                        $goodsdb->param ['no_need_attr'] = 'yes';
                    }
                    if (isset ($this->param ['no_need_goods_handle']) && $this->param ['no_need_goods_handle'] == 'yes') {
                        $goodsdb->param ['no_need_goods_handle'] = 'yes';
                    }
                    if (isset ($this->param ['no_more_price'])) {
                        $goodsdb->param ['no_more_price'] = 'yes';
                    }
                    if (isset ($this->param ['site_id'])) {
                        $goodsdb->param ['site_id'] = $this->param ['site_id'];
                    }
                    if (isset ($this->param ['no_need_site']) && $this->param ['no_need_site'] == 'yes') {
                        $goodsdb->param ['no_need_site'] = 'yes';
                    }
                    if (isset ($this->param ['need_cover'])) {
                        $goodsdb->param ['need_cover'] = 'yes';
                    }
                    $goods_info = $goodsdb->ajaxFind();
                    if (!isset ($goods_info ['goods_id'])) {
                        // 产品已删除或者下架
                        $get_param = $this->param;
                        $this->param = array();
                        $this->param ['cart_id'] = $val ['cart_id'];
                        // $this->ajaxDelete();
                    } else {
                        $val ['goods_info'] = $goods_info;
                        $val ['goods_info'] ['cart_id'] = $val ['cart_id'];
                    }
                    break;
                case 'is_install' :
                    if (!isset ($this->param ['no_need_install'])) {
                        $installdb = new Installdb ();
                        $installdb->param ['cart_id'] = $val ['cart_id'];
                        $val ['install_info'] = $installdb->ajaxFind();
                    }
                    break;
                case 'goods_sale_price' :
                    $val ['sum_sale_price'] = number_format($val ['goods_price'] * $val ['goods_num'], 2, '.', '');
                    break;
                case 'pay_state' :
                    $val ['pay_state_text'] = $val ['pay_state'] == 1 ? '已支付' : '未支付';
                    break;
            }
        }
        if (!isset ($this->param ['no_need_returned'])) {
            $returneddb = new Returneddb ();
            $returneddb->param ['goods_sku'] = $val ['goods_sku'];
            $returneddb->param ['order_no'] = $val ['order_no'];
            $val ['returned_info'] = $returneddb->ajaxLog();
            if ($val ['returned_state'] == 1) {
                if ($val ['pack_state'] == 2) {
                    $val ['cart_state_text'] = '等待发货';
                } else {
                    if ($val ['receipt_state'] == 2) {
                        $val ['cart_state_text'] = '等待收货';
                    } else {
                        if ($val ['judge_state'] == 2) {
                            $val ['cart_state_text'] = '未评价';
                        } else {
                            $val ['cart_state_text'] = '已完成';
                        }
                    }
                }
            } else {
                switch ($val ['returned_state']) {
                    case '2' :
                        $val ['cart_state_text'] = '申请退货';
                        break;
                    case '3' :
                        $val ['cart_state_text'] = '退货完成';
                        if ($val ['judge_state'] == 2) {
                            $val ['cart_state_text'] = '未评价';
                        } else {
                            $val ['cart_state_text'] = '已完成';
                        }
                        break;
                    case '4' :
                        $val ['cart_state_text'] = '申请换货';
                        break;
                    case '5' :
                        $val ['cart_state_text'] = '换货完成';
                        if ($val ['judge_state'] == 2) {
                            $val ['cart_state_text'] = '未评价';
                        } else {
                            $val ['cart_state_text'] = '已完成';
                        }
                        break;
                    case '6' :
                        $val ['cart_state_text'] = '换货已发货';
                        break;
                    case '-1' :
                        $val ['cart_state_text'] = '取消退换货';
                        if ($val ['judge_state'] == 2) {
                            $val ['cart_state_text'] = '未评价';
                        } else {
                            $val ['cart_state_text'] = '已完成';
                        }
                        break;
                }
            }
        }
        return $val;
    }
}